<?php

namespace app\stores\controller\miniapp\promotion;

use app\BaseController;
use app\common\logic\TokenLogic;
use app\stores\model\promotion\TeamActivityModel;

class TeamActivity extends BaseController{

    /*推广中的项目*/
    public function list(){
        $page=$this->request->page;
        $limit=$this->request->limit;
        $team_id = $this->request->teamId;

        $where['team_id'] = $team_id;
        $order = ['ta.add_time'=>'desc'];
        $field = 'ta.team_activity_id,ta.team_commission,ta.status as pa_status,a.*,g.sell_time_type,g.sell_start_time,g.sell_end_time,g.cover_img,g.name,g.original_price,g.total_value,g.type,sd.t_lng,sd.t_lat,sd.logo,sd.tel,sd.per_money,sd.address,s.name';
        $return = TeamActivityModel::list($where,$order,$field,$page,$limit);
        return $this->success($return);
    }
    /*修改推广中的项目状态*/
    public function saveStatus(){
        $is_lead = $this->request->isLead;
        $status = $this->request->param('status/d',1);
        $team_activity_id = $this->request->param('team_activity_id/d',0);

        if($is_lead<1) return $this->error('团长才有权限');
        if($team_activity_id<1) return $this->error('活动不存在');

        $t_activity= TeamActivityModel::info(['team_activity_id'=>$team_activity_id,'type'=>1,'status'=>1]);
        if($t_activity===null) return $this->error('活动不存在');

        TeamActivityModel::up(['status'=>$status],['team_activity_id'=>$team_activity_id]);

        return $this->success('修改成功');
    }
    /*修改推广中的项目分佣*/
    public function saveTeamCommission(){
        $is_lead = $this->request->isLead;
        $team_commission = $this->request->param('team_commission/f',0);
        $team_activity_id = $this->request->param('team_activity_id/d',0);

        if($is_lead<1) return $this->error('团长才有权限');
        if($team_activity_id<1) return $this->error('活动不存在');

        $t_activity= TeamActivityModel::info(['team_activity_id'=>$team_activity_id,'type'=>1,'status'=>1]);
        if($t_activity===null) return $this->error('活动不存在');

        TeamActivityModel::up(['team_commission'=>$team_commission],['team_activity_id'=>$team_activity_id]);

        return $this->success('修改成功');
    }
    /*推广产品详情*/
    public function detail(){
        $type = $this->request->param('type/d',1);
        $team_activity_id = $this->request->param('team_activity_id/d',0);

        if($team_activity_id<1) return $this->error('活动不存在');

        $field = "ta.team_activity_id,ta.team_commission,a.*,g.cover_img,g.sell_time_type,g.sell_start_time,g.sell_end_time,g.goods_details,g.purchase_information,g.use_limit,g.use_limit_detail,g.type,g.original_price,g.crossed_price,g.total_value,g.type,s.name as shop_name,sd.logo,sd.per_money,sd.t_lng,sd.t_lat,sd.tel,sd.address";
        $t_activity= TeamActivityModel::info(['team_activity_id'=>$team_activity_id,'type'=>1,'status'=>1],$field,$type);
        if($t_activity===null) return $this->error('活动不存在');
        return $this->success($t_activity);

    }
    public function getQrcode(){
        $shop_id = $this->request->param('shop_id');
        $ta_id = $this->request->param('ta_id');
        $path = $this->request->param('path');

        $ta = TeamActivityModel::info(['team_activity_id'=>$ta_id,'type'=>1,'status'=>1]);
        if(empty($ta)) return $this->error('项目不存在');

        $path .= '?id='.$ta['goods_id'].'&shopId='.$shop_id.'&&pmid='.$this->request->memberId.'&taId='.$ta_id;
        $t = $this->request->time();
        $sign = md5($shop_id.'|'.$path.'|'.$t.'|'. TokenLogic::getAppQrSignToken());
        $url = $this->request->domain().'/stores/miniapp/qrcodeImg?shop_id='.$shop_id.'&t='.$t.'&path='.urlencode($path).'&sign='.$sign;
        return $this->success([
            'img' => $url
        ]);
    }

}

